// 修改密码部分
import { ref } from 'vue'
import { updatePasswordAPI } from '@/api/manager'
import { useUserStore } from '@/stores'
import { showModal, toast } from '@/composables/util'

export const useRepassword = () => {
  const userStore = useUserStore()

  const formDrawerRef = ref(null)
  // console.log(formDrawerRef);

  // const showDrawer = ref(false)
  const form = ref({
    oldpassword: '',
    password: '',
    repassword: '',
  })
  const rules = {
    oldpassword: [{ required: true, message: '旧密码不能为空', trigger: 'blur' }],
    password: [{ required: true, message: '新密码不能为空', trigger: 'blur' }],
    repassword: [{ required: true, message: '确认密码不能为空', trigger: 'blur' }],
  }
  const formRef = ref(null)
  // 设置加载状态防止用户重复点击登录按钮
  const onSubmit = () => {
    formRef.value.validate((valid) => {
      // console.log(valid)
      if (!valid) {
        return false
      }
      // loading.value = true
      formDrawerRef.value.showLoading()
      updatePasswordAPI(form.value)
        .then((res) => {
          console.log(res)
          toast('修改密码成功，请重新登陆')
          userStore.logout()
        })
        .finally(() => {
          // loading.value = false
          formDrawerRef.value.hideLoading()
        })
    })
  }
  const openRePasswordForm = () => formDrawerRef.value.openDrawer()
  return {
    formDrawerRef,
    form,
    rules,
    formRef,
    onSubmit,
    openRePasswordForm,
  }
}
export const useLogout = () => {
  const userStore = useUserStore()
  const logoutFn = () => {
    showModal('是否要退出登录').then((res) => {
      userStore.logout()
      toast('退出登录成功')
    })
  }
  return {
    logoutFn,
  }
}
